package cn.com.qingz.modules.system.service.impl;

import java.util.List;

import cn.com.qingz.modules.system.service.Identity;
import cn.com.qingz.modules.user.model.HouseModel;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.qingz.modules.system.service.GeneralService;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

public abstract class GeneralServiceImpl<T> implements GeneralService<T> {

    @Autowired
    protected Mapper<T> mapper;

    public T get(Object id) {
        return mapper.selectByPrimaryKey(id);
    }

    public void doSave(T t) {
        mapper.insert(t);
    }

    public void doUpdate(T t) {
        mapper.updateByPrimaryKeySelective(t);
    }

    public void doDel(T t) {
        mapper.delete(t);
    }

    public void doDelById(Object id) {
        mapper.deleteByPrimaryKey(id);
    }

    public boolean isExist(Example example) {
        int count = mapper.selectCountByExample(example);
        return count > 0 ? true : false;
    }

    public List<T> queryAll() {
        return mapper.selectAll();
    }

    public void doAddOrUpdate(T t) {
        if (t instanceof Identity){
            Identity entity =   (Identity)t;
            String id = entity.getId();
            if(id == null){
                mapper.insert(t);
            }else{
                mapper.updateByPrimaryKeySelective(t);
            }
        }
    }

}
